package com.wb3.meta.common.plugin;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 基础controller
 * <p>Title:com.wb3.paradise.core.base.BaseController</p>
 * <p>Description:</p>
 * <p>Company:沫兰遗夏的后花园</p>
 *
 * @author 沫兰遗夏
 * @date 2018年2月1日 下午10:29:17
 */
public abstract class BaseController<REQ> {
    /**
     * 以json方式返回ajax请求
     *
     * @param response
     * @param obj
     */
    public void renderJson(HttpServletResponse response, Object obj) {
        render(response, JSONObject.toJSONString(obj),
                "application/json;charset=UTF-8");
    }

    public void render(HttpServletResponse response, String text,
                       String contentType) {
        try {
            response.setContentType(contentType);
            response.getWriter().write(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 列表接口
     *
     * @param req
     * @param response
     */
    public abstract void list(REQ req, HttpServletResponse response);

    /**
     * 明细接口
     *
     * @param req
     * @param response
     */
    public abstract void detail(REQ req, HttpServletResponse response);

    /**
     * 新增or更新接口
     *
     * @param req
     * @param response
     */
    public abstract void saveOrUpdate(REQ req, HttpServletResponse response);

    /**
     * 删除接口
     *
     * @param req
     * @param response
     */
    public abstract void delete(REQ req, HttpServletResponse response);
}
